<template>
  <div>
    <!-- 查询表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="searchForm.goodsName" placeholder="货物名称" clearable />
      </el-form-item>

      <el-form-item label="">
        <el-input v-model="searchForm.storeName" placeholder="所属仓库名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 导出按钮 -->
    <el-button type="warning" round @click="handleExport">导出数据</el-button>
    <div style="height: 20px;"></div>

    <!-- 数据展示区域 -->
    <el-table :data="tableData.resultList" border style="width: 100%" max-height="500">
      <el-table-column prop="id" sortable label="序号" />
      <el-table-column prop="goodsName" sortable label="货物名称" />
      <el-table-column prop="storeName" sortable label="仓库名称" />
      <el-table-column prop="count" sortable label="数量" />
      <el-table-column prop="shelfName" sortable label="货架名称" />
      <el-table-column prop="freezeCount" sortable label="冻结数量" />
      
      <!-- 操作列 -->
      <!-- <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页区域 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="searchCondition.pageNum"
        v-model:page-size="searchCondition.pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { searchStock } from '../api/api.js'
import { ElMessage } from 'element-plus'

// 查询表单数据
const searchForm = reactive({
  goodsName: '',
  storeName: ''
})

// 表格数据
const tableData = reactive({
  resultList: [],
  totalNum: 0
})

// 分页参数
const searchCondition = reactive({
  pageNum: 1,
  pageSize: 10
})

// 加载状态
const loading = ref(false)

// 查询方法
const search = async () => {
  loading.value = true
  try {
    const params = {
      ...searchForm,
      ...searchCondition
    }
    const res = await searchStock(params)
    console.log('查询结果:', res)
    // if (res.code === 200 && res.success) {
    if (res.data.code === 200 ) {
      tableData.resultList = res.data.data.resultList
      tableData.totalNum = res.data.data.totalNum
      console.log('查询成功:', tableData)
      ElMessage.success('查询成功')
    } else {
      ElMessage.error(res.message || '查询失败')
    }
  } catch (error) {
    console.error('查询出错:', error)
    ElMessage.error('查询出错')
  } finally {
    loading.value = false
  }
}

// 初始加载
onMounted(() => {
  search()
})

// 导出方法
const handleExport = () => {
  // 实现导出逻辑
}

// 编辑方法
const handleEdit = (row) => {
  // 实现编辑逻辑
}

// 删除方法
const handleDelete = (row) => {
  // 实现删除逻辑
}

// 分页方法
const handleSizeChange = (val) => {
  searchCondition.pageSize = val
  search()
}

const handleCurrentChange = (val) => {
  searchCondition.pageNum = val
  search()
}
</script>

<style scoped>
 .demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}

.operation-area {
  margin: 15px 0;
}

.pagination-area {
  margin-top: 20px;
  text-align: right;
}
</style>
